var picker;
var picker_two;
let pick_data =[
		{
			value: '0',
			text: '关闭'
		},
		{
			value: '350',
			text: '第一档'
		},
		{
			value: '650',
			text: '第二档'
		},
		{
			value: '1023',
			text: '第三档'
		},
	];
let pick_data_two=[
	{
		value: '0',
		text: '停止'
	},
	{
		value: '1',
		text: '正转'
	},
	{
		value: '2',
		text: '反转'
	},
]	
	
	
mui.ready(function() {
	picker = new mui.PopPicker();
	picker.setData(pick_data);
	picker_two = new mui.PopPicker();
	picker_two.setData(pick_data_two);
});
var led_type = document.getElementsByClassName("deng")[0];
var led_controll = document.getElementById("deng_input");
var feng_type = document.getElementById("feng_img");
var feng_controll = document.getElementById("showpick"); //档位
var dianji_controll = document.getElementById("showpickTwo"); //档位
 

 

document.getElementById("showpick").addEventListener("tap", function() {
	picker.show(selectItems => {
		//发送数据调制第二档
		editFengShan(selectItems[0].value);
		sendFengShanMessage(selectItems[0].value, "风扇档位");
		
	})
});
document.getElementById("showpickTwo").addEventListener("tap",function(){
	picker_two.show(selectItems => {
		//发送数据调制第二档
		editDianji(selectItems[0].value);
		sendDianjiMessage(selectItems[0].value, "电机");
	})
})


//按钮点击
function checkboxOnclick(checkbox) {
	console.log(checkbox.checked);
	//发送灯光数
	if (checkbox.checked) {
		sendLedMessage("0", "打开");
		editDeng("0");
	} else {
		sendLedMessage("1", "关上");
		editDeng("1");
	}	
}


//修改灯的状态
function editDeng(value){
	if(value=="0"){ //打开
		led_type.classList.add("open");
		led_controll.checked = true;
	}else{
		led_type.classList.remove("open");
		led_controll.checked = false;
	}
}
function editDianji(value){
	 for(var i = 0;i <pick_data_two.length;i++){
		 if(value == pick_data_two[i].value){
				dianji_controll.innerHTML=pick_data_two[i].text;		 
				return;
		 }
	 }
	
}
//修改风扇的状态
function editFengShan(value){
	for(let i =0 ;i<pick_data.length;i++){
		if(value == pick_data[i].value){
			if(i==0){
				try{
					feng_type.classList.remove("rank1");
				}catch(e){}
				try{
					feng_type.classList.remove("rank2");
				}catch(e){}
				try{
					feng_type.classList.remove("rank3");
				}catch(e){}
				
			}else{
				feng_type.classList.add("rank"+i);
			}
			feng_controll.innerHTML = pick_data[i].text;
			return;
		}
	}
}



//生成cliedtid
function makeid() {
	var text = "";
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	for (var i = 0; i < 15; i++)
		text += possible.charAt(Math.floor(Math.random() * possible.length));
	return text;
}


//数据模型 {"type":"led","value":"0","msg":"关闭灯光","isState":"PUB"}  //state PUB 为上传数据 SUB为订阅数据   解析PUB  发送SUB
let hostname = "00bdbf1.mqtt.iot.gz.baidubce.com";
let passWord = "Rn2oHHzT8e6dRFSF";
let username = "00bdbf1/wuyangs";
let port = "8884";
let TOPIC = "FENG";
let clientId = makeid();
let connected = false;
let client = new Paho.MQTT.Client(hostname, Number(port), "/mqtt", clientId);
//设置回调方法
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
let options = {
	invocationContext: {
		host: hostname,
		port: port,
		clientId: clientId
	},
	timeout: 5,
	keepAliveInterval: 60,
	cleanSession: true,
	useSSL: true,
	//reconnect: true,
	onSuccess: onConnect,
	onFailure: onFail,
	userName: username,
	password: passWord
};

client.connect(options);
//客户端断开连接时调用
function onConnectionLost(responseObject) {
	connected = false;
}
// 消息到达时调用
function onMessageArrived(message) {
	let data = JSON.parse(message.payloadString);
	let state = data.isState;
	if (state == "PUB") {
		let type = data.type;
		let value = data.value;
		let msg = data.msg;
		if (type == "led") {
			editDeng(value);
		}else if(type == "fengshan"){
			
			editFengShan(value);
		}else if(type == "zhiliu"){
			editDianji(value);
		}
	}



}
//客户端连接时调用
function onConnect(context) {
	console.log("onConnect");
	connected = true;
	client.subscribe(TOPIC);
	sendGetIotData();
	start();
}

function onConnected(reconnect, uri) {
	console.log("onConnected");
	connected = true;
}

function onFail(context) {
	//连接失败
	connected = false;
}

//发送控制信息
function sendMessage(type, value, msg) {
	if (client.isConnected) {
		let messages = {
			"type": type,
			"value": value,
			"msg": msg,
			"isState": "SUB"
		};
		let message = new Paho.MQTT.Message(JSON.stringify(messages));
		message.destinationName = TOPIC;
		client.send(message);
		
	}
	
}
//发送获取设备信息
function sendGetIotData() {
	sendMessage("getData", "", "我要消息");
}

//发送灯光控制信息
function sendLedMessage(value, msg) {
	sendMessage("led", value, msg);
}

//发送风扇控制信息
function sendFengShanMessage(value, msg) {
	sendMessage("fengshan", value, msg);
}

function sendDianjiMessage(value,msg){
	sendMessage("zhiliu", value, msg);
}

function start() {
	window.setInterval(function() {
		//console.log("我是计时器 "+new Date().getTime());
		if (client.isConnected) {
				sendGetIotData();
		}
	}, 15000);
}


 